package com.cqc.manage.controller;

import cn.hutool.core.date.DateUtil;
import com.cqc.manage.dto.*;
import com.cqc.manage.entity.Business;
import com.cqc.manage.entity.MessageBusiness;
import com.cqc.manage.entity.User;
import com.cqc.manage.service.Imp.business.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/business")
@Api(tags = "business" , description = "业务查询")
public class BusinessController {

    private Logger LOG = LoggerFactory.getLogger(BusinessController.class);

    @Autowired
    private BusinessService businessService;

    @Autowired
    private EngineerBusinessService engineerBusinessService;

    @Autowired
    private BusinessInvoiceService businessInvoiceService;

    @Autowired
    private CostStatisticsService costStatisticsService;

    @Autowired
    private MessageBusinessService messageBusinessService;

    @Autowired
    private EngineerDataService engineerDataService;

    @Autowired
    private BusinessChargeNoticeService businessChargeNoticeService;


    /**
     * 获取所有新业务
     */
    @ApiOperation(value = "获取所有业务",notes = "获取所有业务")
    @RequestMapping(value = "/getbusiness",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResultDto getBusiness(@RequestBody BusinessPageDto businessDto) throws ParseException {
        LOG.info("未完成请求数据：{}",businessDto);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd") ;
        Pageable pageable = PageRequest.of(businessDto.getPage()-1, businessDto.getSize());
        Page<Business> query = businessService.query(pageable);
        List<Business> businesses = query.toList();
        List<BusinessDto> businessDtos = new ArrayList<>();
        for(Business business:businesses){
                BusinessDto businessDto1 = new BusinessDto();
                businessDto1.setContractNo(business.getContractNo());
                businessDto1.setClient(business.getClient());
                businessDto1.setEngineer(business.getEngineer());
                businessDto1.setId(business.getId());
                businessDto1.setEndDate(sdf1.format(business.getEndDate()));
                businessDto1.setStartDate(sdf1.format(business.getStartDate()));
                businessDto1.setReportType(business.getReportType());
                businessDto1.setStandard(business.getStandard());
                businessDto1.setSampleCategory(business.getSampleCategory());
                businessDto1.setSampleModel(business.getSampleModel());
                businessDtos.add(businessDto1);

        }
        businessDto.setTotal(query.getTotalElements());
        businessDto.setRows(businessDtos);
        return new JsonResultDto<BusinessPageDto>("success","200",businessDto);
    }


    /**
     * 测试
     * @return
     */
    @RequestMapping(value = "/getasdbusiness",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResultDto Business() throws Exception {
        businessChargeNoticeService.getBusChaNot("E2021050372","1000");
        return new JsonResultDto<Map<String, Map<Integer, List<Float>>>>("标签获取成功","200",null);
    }


    /**
     * 业务分配
     * @param business
     * @param id
     * @return
     */
    @RequestMapping(value = "/businessDistribution",method = RequestMethod.GET)
    public JsonResultDto setBusinessService(@RequestParam("business") Business business,@RequestParam("userId") String id){
        engineerBusinessService.DistributionBusiness(business,id);
        return new JsonResultDto<BusinessPageDto>("200","业务分配成功");
    }


    /**
     * 业务发票查询  查询每个工程师的业务情况BusinessMessage
     *
     * @return
     */
    @ApiOperation(value = "查询所有未完成业务，然后逐个查询每个业务的协议金额和发票金额",notes = "查询所有未完成业务，然后逐个查询每个业务的协议金额和发票金额")
    @RequestMapping(value = "/businessInvoice",method =RequestMethod.GET)
    public JsonResultDto getBusinessInvoice(@RequestParam("username")String username){
        LOG.info("工程师:{}",username);
        businessInvoiceService.getEngineerBusinessInvoice();
        return null;
    }

    /**
     * 工程师业务查询
     * @param engineerBussinessDto
     * @return
     */
    @RequestMapping(value = "/businessMessage",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public EngineerBussinessDto<MessageBusiness> getBusinessMessage(@RequestBody EngineerBussinessDto engineerBussinessDto){
        LOG.info("business/query查询信息:{}",engineerBussinessDto);
        businessInvoiceService.getEngineerBusinessInvoice(engineerBussinessDto);
        LOG.info("business/query查询结果:{}",engineerBussinessDto.getList());
        return engineerBussinessDto;
    }


    /**
     * 查询业务信息
     */
    @ApiOperation(value = "通过合同编号查询业务信息",notes = "通过合同编号查询业务信息")
    @RequestMapping(value = "/byContractNoMessage",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public BusinessDto getBussinessMessageByContractNo(@RequestBody BusinessDto businessDto){
        LOG.info("业务信息查询请求数据:{}",businessDto);
        businessService.getBusiness(businessDto);
        LOG.info("业务信息查询结果:{}",businessDto);
        return businessDto;
    }

    @ApiOperation(value = "同步costStatistics数据",notes = "同步costStatistics数据")
    @RequestMapping(value = "/getCostStatistics",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public void getCostStatistics(@RequestParam("name")String name){
        costStatisticsService.saveData();
    }

    @ApiOperation(value = "获取工程师到款金额",notes = "获取工程师到款金额")
    @RequestMapping(value = "/getEngineerAmount",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResultDto<Map<String, Float>> getCostStatistics(){
        Map<String, Float> allEngineerAmount = costStatisticsService.getAllEngineerAmount();
        return new JsonResultDto<Map<String, Float>>("success","200",allEngineerAmount);
    }


    /**
     *   echarts主页面的表格数据   获取工程师未完成的业务，通过end_data排序
     * @return
     */
    @ApiOperation(value = "获取工程师到款金额",notes = "获取工程师到款金额")
    @RequestMapping(value = "/getNoCompleteBusiness",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResultDto<List<BusinessDto>> getNoCompleteBusiness(@RequestParam("engName") String engName){
        LOG.info("getNoCompleteBusiness is :{}",engName);
        Page<MessageBusiness> noCompleteBussiness = businessService.getNoCompleteBussiness(engName);
        List<BusinessDto> businessDtoList = new ArrayList<>();
        for(MessageBusiness messageBusiness:noCompleteBussiness.getContent()){
            BusinessDto businessDto = new BusinessDto();
            businessDto.setClient(messageBusiness.getClient());
            businessDto.setStandard(messageBusiness.getStandard());
            businessDto.setEndDate(DateUtil.format(messageBusiness.getEndDate(),"yyyy-MM-dd"));
            businessDto.setStartDate(DateUtil.format(messageBusiness.getStartDate(),"yyyy-MM-dd"));
            businessDto.setSampleCategory(messageBusiness.getSampleCategory());
            businessDto.setReportType(messageBusiness.getReportType());
            businessDto.setSampleModel(messageBusiness.getSampleModel());
            businessDto.setEngineer(engName);
            businessDto.setContractNo(messageBusiness.getContractNo());
            businessDtoList.add(businessDto);
        }
        return new JsonResultDto<List<BusinessDto>>("success","200",businessDtoList);
    }


    /**
     * 添加备注
     */
    @ApiOperation(value = "给业务添加备注",notes = "给业务添加备注")
    @RequestMapping(value = "/addBusinessRemark",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResultDto getCostStatistics(@RequestBody BusinessDto businessDto){
        LOG.info("备注请求数据:{}",businessDto);
        MessageBusiness messageBusiness = messageBusinessService.addRemarkBusiness(businessDto);
        if (messageBusiness != null) {
            return new JsonResultDto<Map<String, Float>>("200","success");
        }
        return new JsonResultDto<Map<String, Float>>("204","error");
    }


//    /**
//     * 获取工程师所有未收到款项的业务
//     * @param enginessUserName
//     * @return
//     */
//    @RequestMapping(value = "/businessMessageNoInvoice",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
//    public JsonResultDto getEngineerBusinessNoInvoice(@RequestParam("enginessUserName")String enginessUserName){
//        List<MessageBusiness> engineerBusinessNoInvoice = businessInvoiceService.getEngineerBusinessNoInvoice(enginessUserName);
//        return new JsonResultDto<List<MessageBusiness>>("200","业务分配成功",engineerBusinessNoInvoice);
//    }



}
